#    SUSAN® - Sole of Unix Save ANything
#
#   Copyright (C) 2017-2020 Hechzh Property
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.
message("Entering ${CMAKE_CURRENT_SOURCE_DIR}")

include_directories(${MYSQL_INCLUDE_DIR} ${PostgreSQL_INCLUDE_DIR})

add_definitions(-D_BDB_PRIV_INTERFACE_)

set(LIBSUSANSQL_SRCS
    bvfs.cc
    cats.cc
    sql.cc
    sql_create.cc
    sql_delete.cc
    sql_find.cc
    sql_get.cc
    sql_list.cc
    sql_pooling.cc
    sql_query.cc
    sql_update.cc
)
set(LIBSUSANCATS_SRCS cats_backends.cc)

set(SQLITE_SRCS sqlite.cc ${LIBSUSANCATS_SRCS})

set(MYSQL_SRCS mysql.cc mysql_batch.cc ${LIBSUSANCATS_SRCS})

set(POSTGRESQL_SRCS postgresql.cc postgresql_batch.cc ${LIBSUSANCATS_SRCS})

add_library(susansql SHARED ${LIBSUSANSQL_SRCS})

set_target_properties(
  susansql PROPERTIES VERSION "${SUSAN_NUMERIC_VERSION}"
                       SOVERSION "${SUSAN_VERSION_MAJOR}"
)
target_link_libraries(susansql susancats susan)

# TODO: install default db backend

# if we have only one database backend, we do not create the dummy libsusancats
# but instead we create a symlink to the db backend lib

list(LENGTH db_backends nr_db_backends)

add_library(susancats SHARED ${LIBSUSANCATS_SRCS})
target_link_libraries(susancats susan)
set_target_properties(
  susancats PROPERTIES VERSION ${SUSAN_NUMERIC_VERSION}
                        SOVERSION "${SUSAN_VERSION_MAJOR}"
)
install(TARGETS susancats DESTINATION ${libdir})

if(HAVE_SQLITE3)
  add_library(susancats-sqlite3 MODULE ${SQLITE_SRCS})
  target_link_libraries(susancats-sqlite3 susansql susan sqlite3)
  install(TARGETS susancats-sqlite3 DESTINATION ${backenddir})

  install(
    DIRECTORY ddl
    DESTINATION ${scriptdir}
    FILES_MATCHING
    PATTERN *sqlite3*.sql
  )
endif()

if(HAVE_MYSQL)
  add_library(susancats-mysql MODULE ${MYSQL_SRCS})
  target_link_libraries(susancats-mysql susansql susan ${MYSQL_LIBRARIES})
  install(TARGETS susancats-mysql DESTINATION ${backenddir})

  install(
    DIRECTORY ddl
    DESTINATION ${scriptdir}
    FILES_MATCHING
    PATTERN *mysql*.sql
  )
endif()

if(HAVE_POSTGRESQL)
  add_library(susancats-postgresql MODULE ${POSTGRESQL_SRCS})
  target_link_libraries(
    susancats-postgresql susansql susan ${PostgreSQL_LIBRARY}
  )
  install(TARGETS susancats-postgresql DESTINATION ${backenddir})

  install(
    DIRECTORY ddl
    DESTINATION ${scriptdir}
    FILES_MATCHING
    PATTERN *postgresql*.sql
  )
endif()

install(TARGETS susansql DESTINATION ${libdir})

install(
  FILES create_susan_database
        update_susan_tables
        make_susan_tables
        grant_susan_privileges
        drop_susan_tables
        drop_susan_database
        make_catalog_backup.pl
        make_catalog_backup
        delete_catalog_backup
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
              WORLD_READ WORLD_EXECUTE
  DESTINATION ${scriptdir}
)

install(
  DIRECTORY ddl
  DESTINATION ${scriptdir}
  PATTERN version.map
  PATTERN *.in EXCLUDE
  PATTERN *.sql EXCLUDE
)

if(${nr_db_backends} EQUAL 1)
  install(CODE "set(libdir \"${libdir}\")")
  install(CODE "set(backenddir \"${backenddir}\")")
  install(CODE "set(default_db_backend \"${default_db_backend}\")")
  install(
    CODE "set(CMAKE_SHARED_LIBRARY_SUFFIX \"${CMAKE_SHARED_LIBRARY_SUFFIX}\")"
  )
  install(CODE "set(VERSION \"${SUSAN_NUMERIC_VERSION}\")")
  install(CODE "set(SRC_DIR \"${PROJECT_SOURCE_DIR}\")")

  install(
    SCRIPT ${PROJECT_SOURCE_DIR}/cmake/susan-symlink-default-db-backend.cmake
  )
endif()
